<?php

namespace app\common\model\mysql;

use think\Model;

class Category extends Model
{

    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     *
     * @param $field
     * @return Category[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalCategorys($field = "*")
    {
        $where = [
            "status" => config("status.mysql.table_normal")
        ];

        $order = [
            'listorder' => "desc",
            'id' => 'desc'
        ];
        $result = $this->field($field)
            ->where($where)
            ->order($order)
            ->select();
        return $result;
    }


    public function getLists($where, $num = 5)
    {
        $order = [
            'listorder' => "desc",
            'id' => 'desc'
        ];

        $result = $this->where("status","<>",config("status.mysql.table_delete"))
            ->where($where)
            ->order($order)
            ->paginate($num);
        return $result;
    }

    /**
     * 根据ID更新库里面的数据
     * @param $id
     * @param $data
     * @return void
     */
    public function updateById($id, $data)
    {
        $data['update_time']  = time();
        return $this->where(["id" => $id])->save($data);
    }

    /**
     * @param $condition
     * @return mixed
     */
    public function getChildCountInPids($condition)
    {
        $where[] = ["pid","in",$condition];
        $where[] = ["status","<>",config("status.mysql.table_delete")];
        $res = $this->where($where)
            ->field(["pid","count(*) as count"])
            ->group("pid")
            ->select();

//        echo $this->getLastSql();
//        exit();
        return $res;
    }
}